

//variable init
var settings = {
	mode : "system",
	//pac mode
	pac_url : "http://127.0.0.1:10002/proxy.pac",
};
//fixed mode
var fixedModeSettings = {
	isEnable : false,
	bypass_list : [
		"localhost",
		"127.*",
		"10.*",
		"172.16.*",
		"172.17.*",
		"172.18.*",
		"172.19.*",
		"172.20.*",
		"172.21.*",
		"172.22.*",
		"172.23.*",
		"172.24.*",
		"172.25.*",
		"172.26.*",
		"172.27.*",
		"172.28.*",
		"172.29.*",
		"172.30.*",
		"172.31.*",
		"192.168.*",
		"*.12306.cn",
		"*.51ym.me",
		"*.52pojie.cn",
		"*.8686c.com",
		"*.abercrombie.com",
		"*.adobesc.com",
		"*.air-matters.com",
		"*.air-matters.io",
		"*.airtable.com",
		"*.akadns.net",
		"*.apache.org",
		"*.api.crisp.chat",
		"*.api.termius.com",
		"*.appshike.com",
		"*.appstore.com",
		"*.aweme.snssdk.com",
		"*.bababian.com",
		"*.battle.net",
		"*.beatsbydre.com",
		"*.bet365.com",
		"*.bilibili.cn",
		"*.ccgslb.com",
		"*.ccgslb.net",
		"*.chunbo.com",
		"*.chunboimg.com",
		"*.clashroyaleapp.com",
		"*.cloudsigma.com",
		"*.cloudxns.net",
		"*.cmfu.com",
		"*.culturedcode.com",
		"*.dct-cloud.com",
		"*.didialift.com",
		"*.douyutv.com",
		"*.duokan.com",
		"*.dytt8.net",
		"*.easou.com",
		"*.ecitic.net",
		"*.eclipse.org",
		"*.eudic.net",
		"*.ewqcxz.com",
		"*.fir.im",
		"*.frdic.com",
		"*.fresh-ideas.cc",
		"*.godic.net",
		"*.goodread.com",
		"*.haibian.com",
		"*.hdslb.net",
		"*.hollisterco.com",
		"*.hongxiu.com",
		"*.hxcdn.net",
		"*.images.unsplash.com",
		"*.img4me.com",
		"*.ipify.org",
		"*.ixdzs.com",
		"*.jd.hk",
		"*.jianshuapi.com",
		"*.jomodns.com",
		"*.jsboxbbs.com",
		"*.knewone.com",
		"*.kuaidi100.com",
		"*.lemicp.com",
		"*.letvcloud.com",
		"*.lizhi.io",
		"*.localizecdn.com",
		"*.lucifr.com",
		"*.luoo.net",
		"*.mai.tn",
		"*.maven.org",
		"*.miwifi.com",
		"*.moji.com",
		"*.moke.com",
		"*.mtalk.google.com",
		"*.mxhichina.com",
		"*.myqcloud.com",
		"*.myunlu.com",
		"*.netease.com",
		"*.nfoservers.com",
		"*.nssurge.com",
		"*.nuomi.com",
		"*.ourdvs.com",
		"*.overcast.fm",
		"*.paypal.com",
		"*.paypalobjects.com",
		"*.pgyer.com",
		"*.qdaily.com",
		"*.qdmm.com",
		"*.qin.io",
		"*.qingmang.me",
		"*.qingmang.mobi",
		"*.qqurl.com",
		"*.rarbg.to",
		"*.rrmj.tv",
		"*.ruguoapp.com",
		"*.sm.ms",
		"*.snwx.com",
		"*.soku.com",
		"*.startssl.com",
		"*.store.steampowered.com",
		"*.symcd.com",
		"*.teamviewer.com",
		"*.tmzvps.com",
		"*.trello.com",
		"*.trellocdn.com",
		"*.ttmeiju.com",
		"*.udache.com",
		"*.uxengine.net",
		"*.weather.bjango.com",
		"*.weather.com",
		"*.webqxs.com",
		"*.weico.cc",
		"*.wenku8.net",
		"*.werewolf.53site.com",
		"*.windowsupdate.com",
		"*.wkcdn.com",
		"*.workflowy.com",
		"*.xdrig.com",
		"*.xiaojukeji.com",
		"*.xiaomi.net",
		"*.xiaomicp.com",
		"*.ximalaya.com",
		"*.xitek.com",
		"*.xmcdn.com",
		"*.xslb.net",
		"*.xteko.com",
		"*.yach.me",
		"*.yixia.com",
		"*.yunjiasu-cdn.net",
		"*.zealer.com",
		"*.zgslb.net",
		"*.zimuzu.tv",
		"*.zmz002.com",
		"*.samsungdm.com"
	],
	singleProxySetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10808
	}
};

//advanced fixed mode
var advancedFixedModeSettings = {
	isEnable : true,
	advanced_fixed_mode_bypass_list : [
		"localhost",
		"127.*",
		"10.*",
		"172.16.*",
		"172.17.*",
		"172.18.*",
		"172.19.*",
		"172.20.*",
		"172.21.*",
		"172.22.*",
		"172.23.*",
		"172.24.*",
		"172.25.*",
		"172.26.*",
		"172.27.*",
		"172.28.*",
		"172.29.*",
		"172.30.*",
		"172.31.*",
		"192.168.*",
		"*.12306.cn",
		"*.51ym.me",
		"*.52pojie.cn",
		"*.8686c.com",
		"*.abercrombie.com",
		"*.adobesc.com",
		"*.air-matters.com",
		"*.air-matters.io",
		"*.airtable.com",
		"*.akadns.net",
		"*.apache.org",
		"*.api.crisp.chat",
		"*.api.termius.com",
		"*.appshike.com",
		"*.appstore.com",
		"*.aweme.snssdk.com",
		"*.bababian.com",
		"*.battle.net",
		"*.beatsbydre.com",
		"*.bet365.com",
		"*.bilibili.cn",
		"*.ccgslb.com",
		"*.ccgslb.net",
		"*.chunbo.com",
		"*.chunboimg.com",
		"*.clashroyaleapp.com",
		"*.cloudsigma.com",
		"*.cloudxns.net",
		"*.cmfu.com",
		"*.culturedcode.com",
		"*.dct-cloud.com",
		"*.didialift.com",
		"*.douyutv.com",
		"*.duokan.com",
		"*.dytt8.net",
		"*.easou.com",
		"*.ecitic.net",
		"*.eclipse.org",
		"*.eudic.net",
		"*.ewqcxz.com",
		"*.fir.im",
		"*.frdic.com",
		"*.fresh-ideas.cc",
		"*.godic.net",
		"*.goodread.com",
		"*.haibian.com",
		"*.hdslb.net",
		"*.hollisterco.com",
		"*.hongxiu.com",
		"*.hxcdn.net",
		"*.images.unsplash.com",
		"*.img4me.com",
		"*.ipify.org",
		"*.ixdzs.com",
		"*.jd.hk",
		"*.jianshuapi.com",
		"*.jomodns.com",
		"*.jsboxbbs.com",
		"*.knewone.com",
		"*.kuaidi100.com",
		"*.lemicp.com",
		"*.letvcloud.com",
		"*.lizhi.io",
		"*.localizecdn.com",
		"*.lucifr.com",
		"*.luoo.net",
		"*.mai.tn",
		"*.maven.org",
		"*.miwifi.com",
		"*.moji.com",
		"*.moke.com",
		"*.mtalk.google.com",
		"*.mxhichina.com",
		"*.myqcloud.com",
		"*.myunlu.com",
		"*.netease.com",
		"*.nfoservers.com",
		"*.nssurge.com",
		"*.nuomi.com",
		"*.ourdvs.com",
		"*.overcast.fm",
		"*.paypal.com",
		"*.paypalobjects.com",
		"*.pgyer.com",
		"*.qdaily.com",
		"*.qdmm.com",
		"*.qin.io",
		"*.qingmang.me",
		"*.qingmang.mobi",
		"*.qqurl.com",
		"*.rarbg.to",
		"*.rrmj.tv",
		"*.ruguoapp.com",
		"*.sm.ms",
		"*.snwx.com",
		"*.soku.com",
		"*.startssl.com",
		"*.store.steampowered.com",
		"*.symcd.com",
		"*.teamviewer.com",
		"*.tmzvps.com",
		"*.trello.com",
		"*.trellocdn.com",
		"*.ttmeiju.com",
		"*.udache.com",
		"*.uxengine.net",
		"*.weather.bjango.com",
		"*.weather.com",
		"*.webqxs.com",
		"*.weico.cc",
		"*.wenku8.net",
		"*.werewolf.53site.com",
		"*.windowsupdate.com",
		"*.wkcdn.com",
		"*.workflowy.com",
		"*.xdrig.com",
		"*.xiaojukeji.com",
		"*.xiaomi.net",
		"*.xiaomicp.com",
		"*.ximalaya.com",
		"*.xitek.com",
		"*.xmcdn.com",
		"*.xslb.net",
		"*.xteko.com",
		"*.yach.me",
		"*.yixia.com",
		"*.yunjiasu-cdn.net",
		"*.zealer.com",
		"*.zgslb.net",
		"*.zimuzu.tv",
		"*.zmz002.com",
		"*.samsungdm.com"
	],
	proxyForHttpSetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10809
	},
	proxyForHttpsSetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10809
	},
	proxyForFtpSetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10808
	},
	fallbackProxySetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10808
	}
};
/*

*/
//intelligent mode
var intelligentModeSettings = {
	//初始化
	init : false ,
	//file list
	//直连域名列表
	directListFile_isEnable : true ,
	directListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/direct-list.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/direct-list.txt"
	],
	//代理域名列表
	proxyListFile_isEnable : true ,
	proxyListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/proxy-list.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/proxy-list.txt"
	],
	//广告列表
	adListFile_isEnable : false ,
	adListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/reject-list.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/reject-list.txt"
	],
	//@felixonmars/dnsmasq-china-list 仓库收集的在中国大陆可直连的域名列表
	chinaListFile_isEnable : false ,
	chinaListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/china-list.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/china-list.txt"
	],
	//Apple 在中国大陆可直连的域名列表
	appleCNListFile_isEnable : false ,
	appleCNListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/apple-cn.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/apple-cn.txt"
	],
	//Google 在中国大陆可直连的域名列表 
	googleCNListFile_isEnable : false ,
	googleCNListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/google-cn.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/google-cn.txt"
	],
	//GFWList 域名列表 
	gfwListFile_isEnable : true ,
	gfwListFile : [
		"https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/gfw.txt",
		"https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/gfw.txt"
	],
	//url list
	isEnable : null,
	intelligent_mode_default_bypass_list : [    
		"localhost",
		"127.*",
		"10.*",
		"172.16.*",
		"172.17.*",
		"172.18.*",
		"172.19.*",
		"172.20.*",
		"172.21.*",
		"172.22.*",
		"172.23.*",
		"172.24.*",
		"172.25.*",
		"172.26.*",
		"172.27.*",
		"172.28.*",
		"172.29.*",
		"172.30.*",
		"172.31.*",
		"192.168.*",
		"*.12306.cn",
		"*.51ym.me",
		"*.52pojie.cn",
		"*.8686c.com",
		"*.abercrombie.com",
		"*.adobesc.com",
		"*.air-matters.com",
		"*.air-matters.io",
		"*.airtable.com",
		"*.akadns.net",
		"*.apache.org",
		"*.api.crisp.chat",
		"*.api.termius.com",
		"*.appshike.com",
		"*.appstore.com",
		"*.aweme.snssdk.com",
		"*.bababian.com",
		"*.battle.net",
		"*.beatsbydre.com",
		"*.bet365.com",
		"*.bilibili.cn",
		"*.ccgslb.com",
		"*.ccgslb.net",
		"*.chunbo.com",
		"*.chunboimg.com",
		"*.clashroyaleapp.com",
		"*.cloudsigma.com",
		"*.cloudxns.net",
		"*.cmfu.com",
		"*.culturedcode.com",
		"*.dct-cloud.com",
		"*.didialift.com",
		"*.douyutv.com",
		"*.duokan.com",
		"*.dytt8.net",
		"*.easou.com",
		"*.ecitic.net",
		"*.eclipse.org",
		"*.eudic.net",
		"*.ewqcxz.com",
		"*.fir.im",
		"*.frdic.com",
		"*.fresh-ideas.cc",
		"*.godic.net",
		"*.goodread.com",
		"*.haibian.com",
		"*.hdslb.net",
		"*.hollisterco.com",
		"*.hongxiu.com",
		"*.hxcdn.net",
		"*.images.unsplash.com",
		"*.img4me.com",
		"*.ipify.org",
		"*.ixdzs.com",
		"*.jd.hk",
		"*.jianshuapi.com",
		"*.jomodns.com",
		"*.jsboxbbs.com",
		"*.knewone.com",
		"*.kuaidi100.com",
		"*.lemicp.com",
		"*.letvcloud.com",
		"*.lizhi.io",
		"*.localizecdn.com",
		"*.lucifr.com",
		"*.luoo.net",
		"*.mai.tn",
		"*.maven.org",
		"*.miwifi.com",
		"*.moji.com",
		"*.moke.com",
		"*.mtalk.google.com",
		"*.mxhichina.com",
		"*.myqcloud.com",
		"*.myunlu.com",
		"*.netease.com",
		"*.nfoservers.com",
		"*.nssurge.com",
		"*.nuomi.com",
		"*.ourdvs.com",
		"*.overcast.fm",
		"*.paypal.com",
		"*.paypalobjects.com",
		"*.pgyer.com",
		"*.qdaily.com",
		"*.qdmm.com",
		"*.qin.io",
		"*.qingmang.me",
		"*.qingmang.mobi",
		"*.qqurl.com",
		"*.rarbg.to",
		"*.rrmj.tv",
		"*.ruguoapp.com",
		"*.sm.ms",
		"*.snwx.com",
		"*.soku.com",
		"*.startssl.com",
		"*.store.steampowered.com",
		"*.symcd.com",
		"*.teamviewer.com",
		"*.tmzvps.com",
		"*.trello.com",
		"*.trellocdn.com",
		"*.ttmeiju.com",
		"*.udache.com",
		"*.uxengine.net",
		"*.weather.bjango.com",
		"*.weather.com",
		"*.webqxs.com",
		"*.weico.cc",
		"*.wenku8.net",
		"*.werewolf.53site.com",
		"*.windowsupdate.com",
		"*.wkcdn.com",
		"*.workflowy.com",
		"*.xdrig.com",
		"*.xiaojukeji.com",
		"*.xiaomi.net",
		"*.xiaomicp.com",
		"*.ximalaya.com",
		"*.xitek.com",
		"*.xmcdn.com",
		"*.xslb.net",
		"*.xteko.com",
		"*.yach.me",
		"*.yixia.com",
		"*.yunjiasu-cdn.net",
		"*.zealer.com",
		"*.zgslb.net",
		"*.zimuzu.tv",
		"*.zmz002.com",
		"*.samsungdm.com"
	],
	intelligent_mode_direct_list : [],
	intelligent_mode_proxy_list : [],
	intelligent_mode_block_list : [],
	proxySetting : {
		scheme:"socks5",
		host:"127.0.0.1",
		port:10808
	},
	//列表总成
	mainList : [],
	//生成的代理文件内容
	pacScript : "",

};

// function
//选择代理
function selectProxy(proxyMode,callback){
	console.debug("function selectProxy start , proxyMode: " + proxyMode);
    switch (proxyMode) {
		case "direct":
			noProxy(callback);
		break;
		case "auto_detect":
			autoDetectProxy(callback);
		break;
		case "pac_script":
			pacScriptProxy(settings.pac_url,callback);
		break;
		case "fixed_servers":
			if (advancedFixedModeSettings.isEnable) {
				getAdvancedProxySetting(callback);
			} else {
				getSimpleProxySetting(callback);
			}
		break;
		case "intelligent":
			getIntelligentProxySetting(proxySettings,callback);
			break;
		case "system":
			useSystemProxy(callback);
			break;
		default:
			useSystemProxy(callback);
		break;
	}
	console.debug("function selectProxy end");
}
//保存设置
async function saveProxySetting(callback){
	console.debug("function saveProxySetting start");
	//保存 settings
	await chrome.storage.local.set({ 
		"settings": settings,
		"fixedModeSettings": fixedModeSettings,
		"advancedFixedModeSettings": advancedFixedModeSettings,
		"intelligentModeSettings": intelligentModeSettings
	},() => {
	    console.debug("settings is set, value: " , settings);
		console.debug("fixedModeSettings is set, value: " , fixedModeSettings);
		console.debug("advancedFixedModeSettings is set, value: " , advancedFixedModeSettings);
		console.debug("intelligentModeSettings is set, value: " , intelligentModeSettings);
	});
	console.debug("function saveProxySetting end");
}
//读取保存的设置
async function getProxySetting(callback){
    console.debug("function getProxySetting start");
	var  result = await chrome.storage.local.get(["settings","fixedModeSettings","advancedFixedModeSettings","intelligentModeSettings"]);
	console.debug("getProxySetting result=",result);
	if (result?.settings != null && result.settings != undefined) {
		settings = result.settings;
		console.debug("settings =",result.settings);
	}
	if (result?.fixedModeSettings != null && result.fixedModeSettings != undefined) {
		fixedModeSettings = result.fixedModeSettings;
		console.debug("fixedModeSettings=",result.fixedModeSettings);
	}
	if (result?.advancedFixedModeSettings != null && result.advancedFixedModeSettings != undefined) {
		advancedFixedModeSettings = result.advancedFixedModeSettings;
		console.debug("advancedFixedModeSettings =",result.advancedFixedModeSettings);
	}
	if (result?.intelligentModeSettings != null && result.intelligentModeSettings != undefined) {
		intelligentModeSettings = result.intelligentModeSettings;
		console.debug("intelligentModeSettings =",result.intelligentModeSettings);
	}
	console.debug( "function getProxySetting end" );
}
//清除设置内容
async function clearProxySetting(){
    await chrome.storage.local.clear();
}


//使用系统代理
function useSystemProxy(callback){
	console.debug("function useSystemProxy start");
	var proxySetting = {
		value: {mode: "system"},
		scope: 'regular'
	}
	chrome.proxy.settings.set(proxySetting,callback);
	console.debug("function useSystemProxy end");
}

//直连模式
function noProxy(callback){
	console.debug("function noProxy start");
	var proxySetting = {
		value: {mode: "direct"},
		scope: 'regular'
	}
	chrome.proxy.settings.set(proxySetting,callback);
	console.debug("function noProxy end");
}

//自动感知代理
function autoDetectProxy(callback){
	console.debug("function autoDetectProxy start");
	var proxySetting = {
		value: {mode: "auto_detect"},
		scope: 'regular'
	}
	chrome.proxy.settings.set(proxySetting,callback);
	console.debug("function autoDetectProxy end");
}

//代理自动配置文件
function pacScriptProxy(scriptPath,callback){
	console.debug("function pacScriptProxy start");
	var proxySetting = {
		value: {
			mode: "pac_script",
			pacScript: {
				data: scriptPath,
				mandatory:false
			}
		},
		scope: 'regular'
	}
	chrome.proxy.settings.set(proxySetting,callback);
	console.debug("function pacScriptProxy end");
}

//手动设置代理
function fixedServersProxy(rules,callback){
	console.debug("function fixedServersProxy start parameter rules = " , rules ,"\n" , "callback = " , callback);
	var proxySetting = {
		value: {
			mode: "fixed_servers",
			rules: rules
		},
		scope: 'regular'
	}
	console.debug("function fixedServersProxy --- proxySetting = " , proxySetting);
	chrome.proxy.settings.set(proxySetting,callback);
	console.debug("function fixedServersProxy end");
}
//获取简易设置
function getSimpleProxySetting(callback){
	console.debug("function getSimpleProxySetting start");
	var simpleProxyRules = {
		singleProxy:fixedModeSettings.singleProxySetting,
		bypassList: fixedModeSettings.bypass_list
	}
	fixedServersProxy(simpleProxyRules,callback);
	console.debug("function getSimpleProxySetting end simpleProxyRules = ", simpleProxyRules );
}
//获取详细设置
function getAdvancedProxySetting(callback){
	console.debug("function getAdvancedProxySetting start");	
	advancedProxyRules = {
	    proxyForHttp : advancedFixedModeSettings.proxyForHttpSetting,
	    proxyForHttps : advancedFixedModeSettings.proxyForHttpsSetting,
		proxyForFtp : advancedFixedModeSettings.proxyForFtpSetting,
		fallbackProxy : advancedFixedModeSettings.fallbackProxySetting,
		bypassList: advancedFixedModeSettings.advanced_fixed_mode_bypass_list
	}
	fixedServersProxy(advancedProxyRules,callback);
	console.debug("function getAdvancedProxySetting end");
}
